﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Secure Text Document Class
 *      Provides an implementation for processing secure text files.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using Adaptive.Foundation;

namespace Adaptive.Foundation.IO
{
    /// <summary>
    /// Provides an implementation for processing secure text files.
    /// </summary>
    public sealed class SecureTextDocument : DisposableObjectBase, IDocument
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Secure Text file reader/writer object.
        /// </summary>
        private SecureDocument _textFile;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public SecureTextDocument()
            : base()
        {
            _textFile = new SecureDocument();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_textFile != null)
                    _textFile.Dispose();
            }

            _textFile = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets a value indicating whether the file has been loaded for editing.
        /// </summary>
        /// <value>
        /// <b>true</b> if the document is considered to be open; otherwise, <b>false</b>.
        /// </value>
        public bool IsOpen
        {
            get
            {
                return ((_textFile.Text != null) && (_textFile.Text.Length > 0));
            }
        }
        /// <summary>
        /// Gets or sets the name of the file to be read or saved.
        /// </summary>
        /// <value>
        /// A fully-qualified path and file name.
        /// </value>
        public string FileName
        {
            get
            {
                return _textFile.FileName;
            }
            set
            {
                _textFile.FileName = value;
            }
        }
        /// <summary>
        /// Gets or sets the text being stored in the file.
        /// </summary>
        /// <value>
        /// The text content read from a file and/or to be stored in a file.
        /// </value>
        public string Text
        {
            get
            {
                return _textFile.Text;
            }
            set
            {
                _textFile.Text = value;
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Sets the password to use when encrypting or decrypting the file.
        /// </summary>
        /// <param name="password">
        /// A user-supplied password.
        /// </param>
        public void SetPassword(string password)
        {
            _textFile.SetPassword(password);
        }
        /// <summary>
        /// Loads the content of the document into memory.
        /// </summary>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, <b>false</b>.
        /// </returns>
        public bool Load()
        {
            bool returnValue = false;               //Return value.

            returnValue = _textFile.LoadDocument();
            return returnValue;
        }
        /// <summary>
        /// Saves the content of the document to disk.
        /// </summary>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, <b>false</b>.
        /// </returns>
        public bool Save()
        {
            bool returnValue = false;               //Return value.

            returnValue = _textFile.SaveDocument();
            return returnValue;
        }
        #endregion
    }
}